/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.User;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

/**
 *
 * @author daz
 */
public class UserDao extends SimpleJdbcDaoSupport {

    public void insertUser(String name, String pass, String age, String address, String phone, String email, String sex) {
        String query =
                "INSERT INTO bklib.`user` "
                + "(NAME,PASSWORD,AGE,ADDRESS,TELEPHONE,EMAIL,SEX) VALUES "
                + "('" + name + "','" + pass + "','" + age + "','" + address + "','" + phone + "','" + email + "','" + sex + "');";
        getJdbcTemplate().execute(query);
    }

    public List<User> searchUser(String name) {
        String query = "SELECT * FROM bklib.`user` u where name='" + name + "'";
        return getSimpleJdbcTemplate().query(query, new UserMapper());
    }

    public List<User> searchUser(int userId) {
        String query = "SELECT * FROM bklib.`user` u where id=" + userId;
        return getSimpleJdbcTemplate().query(query, new UserMapper());
    }

    private static class UserMapper implements ParameterizedRowMapper<User> {

        public User mapRow(ResultSet rs, int rowNum) throws SQLException {
            User user = new User();
            user.setAddress(rs.getString("address"));
            user.setAge(rs.getString("age"));
            user.setName(rs.getString("name"));
            user.setPass(rs.getString("password"));
            user.setId(rs.getInt("id"));
            return user;
        }
    }
}
